package com.meiqiongproperty.yiguanjia.fragment.home.life;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.activity.GenericFragmentActivity;
import com.meiqiongproperty.yiguanjia.databinding.DailyCleannerLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendLifeServiceDetail;
import com.meiqiongproperty.yiguanjia.httpbean.life.LifeServiceDetailBean;
import com.meiqiongproperty.yiguanjia.ui.adbanner.animations.DescriptionAnimation;
import com.meiqiongproperty.yiguanjia.ui.adbanner.indicators.PagerIndicator;
import com.meiqiongproperty.yiguanjia.ui.adbanner.slidertypes.BaseSliderView;
import com.meiqiongproperty.yiguanjia.ui.adbanner.slidertypes.TextSliderView;
import com.meiqiongproperty.yiguanjia.ui.adbanner.viewpagerswitchanimation.SliderLayout;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.List;

/**
 * author: Administrator
 * date: 2017/5/5.
 * package:com.meiqiongproperty.yiguanjia.fragment.home.life
 */

public class DailyCleannerFragment extends BaseFragment {
    DailyCleannerLayoutBinding binding;
    String id;
    String price;
    String ServiceId;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.daily_cleanner_layout, container, false);
        binding.setClick(this);
        savedInstanceState = getArguments();
        if (savedInstanceState != null) {
            id = savedInstanceState.getString("id");
        }

        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        binding.navigation.setTitle("日常保洁");
        getServiceDetail();
        return binding.getRoot();
    }

    public void sumitOrder(View view) {
        Bundle bundle = new Bundle();
        bundle.putString("price", price);
        bundle.putString("id", id);
        GenericFragmentActivity.start(getActivity(), DailyCleannerOrderPayFragment.class, bundle);
    }

    /**
     * 获取列表服务详情数据
     */
    public void getServiceDetail() {
        SendLifeServiceDetail send = new SendLifeServiceDetail();
        send.setId(id + "");
        send.setToken(Utils.getToken(getActivity()));
        send.setRoomID(Utils.getRoomID(getActivity()));

        ApiService.getLifeServiceDetail(getActivity(), new IHttpCallback<LifeServiceDetailBean>() {
            @Override
            public void onNext(LifeServiceDetailBean lifeServiceDetailBean) {
                binding.lifeServiceDetailContent.setText(lifeServiceDetailBean.getService().getIntroduction());
                binding.lifeServicePrompt.setText(lifeServiceDetailBean.getService().getPrompt());
                binding.lifeServicePrice.setText("￥" + lifeServiceDetailBean.getService().getPrice());
                binding.lifeServiceUnit.setText("/" + lifeServiceDetailBean.getService().getUnit());
                List<String> list = lifeServiceDetailBean.getService().getLogo();
                getData1(list);
                //  serviceTime=
                price = lifeServiceDetailBean.getService().getPrice();
            }

            @Override
            public void onError(int code, String message) {

            }
        }, Utils.getRequestBody(send));
    }


    public void getData1(final List<String> img) {
        Handler handler = new Handler();
        if (isAdded()) {
            binding.slider.getPagerIndicator().setDefaultIndicatorColor(getActivity().getResources().getColor(R.color.transparent), Color.argb(1, 255, 255, 255));
        }
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                binding.slider.stopAutoCycle();
            }
        }, 200);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (img.size() <= 1) {
                    binding.slider.setBottomIndicator(View.GONE);
                    binding.slider.getPagerIndicator().setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Invisible);
                } else {
                    binding.slider.setBottomIndicator(View.VISIBLE);
                    binding.slider.getPagerIndicator().setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Visible);
                    if (isAdded()){
                        binding.slider.getPagerIndicator().setDefaultIndicatorColor(getActivity().getResources().getColor(R.color.header_color), Color.argb(66, 255, 255, 255));
                    }
                }

                if (img.size() <= 1) {
                    binding.slider.stopAutoCycle();
                } else {
                    binding.slider.startAutoCycle();
                }
            }
        }, img.size() * 1000);

        for (int i = 0; i < img.size(); i++) {
            TextSliderView textSliderView = new TextSliderView(getActivity(), img);
            // initialize a SliderLayout
            textSliderView
                    .image(img.get(i))
                    .setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                        @Override
                        public void onSliderClick(BaseSliderView slider) {

                        }
                    });

            binding.slider.addSlider(textSliderView);
        }
        // binding.customIndicator.setColor(Color.rgb(19, 181, 221),Color.argb(33, 255, 255, 255));
        binding.slider.setPresetTransformer(SliderLayout.Transformer.Stack);
        binding.slider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        binding.slider.setCustomAnimation(new DescriptionAnimation());

    }
}
